﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ERP.Manager.DataEntity;
using ERP.Manager.BusinessServices;
using ERP.Manager.Controllers.CustomAttributes;

namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class DepartmentController : Controller
    {
        private readonly DepartmentService departmentservice;
        public DepartmentController() { }
        public DepartmentController(DepartmentService departmentService)
        {
            this.departmentservice = departmentService;
        }

        #region 显示所有部门信息的页面
        public ActionResult ShowDepartment()
        {
            ViewData["department"] = departmentservice.getDepartments();
            return View();
        }
        #endregion

        #region 添加部门信息
        [CheckPersonnel]
        public ActionResult AddDepartment()
        {
            return View();
        }
        #endregion

        #region 修改部门信息页面
        [CheckPersonnel]
        public ActionResult Updatedepartment(string departmentId)
        {
            DepartmentInfo departmentinfo = departmentservice.DetailDepartment(departmentId);
            ViewData["department"] = departmentinfo;
            return View();
        }
        #endregion

        #region Ajax添加部门信息
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxAddDepartment(string departmentID, string departmentName, string departmentnote)
        {
            try
            {
                DepartmentInfo department = new DepartmentInfo();
                department.departmentId = departmentID;
                department.departmentName = departmentName;
                department.departmentNote = departmentnote;
                int rowcount = departmentservice.addDepartment(department);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return "" + ex.Message;
            }
        }
        #endregion

        #region ajax修改部门信息
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxUpdatedepartment(string departmentID, string departmentName, string departmentNote)
        {
            try
            {
                DepartmentInfo department = new DepartmentInfo();
                department.departmentId = departmentID;
                department.departmentName = departmentName;
                department.departmentNote = departmentNote;
                int roucount = departmentservice.UpdateDepartment(department);
                if (roucount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return "" + ex.Message;
            }
        }
        #endregion

        #region ajax验证部门编号是否存在
        public string AjaxdepartmentIDValidator(string departmentID)
        {
            DepartmentInfo department = departmentservice.DetailDepartment(departmentID);
            if (department != null && department.departmentId == departmentID)
            {
                System.Threading.Thread.Sleep(1000);
                return "error";
            }
            else
            {
                System.Threading.Thread.Sleep(1000);
                return "success";
            }
        }
        #endregion

        #region ajax验证部门名称是否存在
        public string AjaxdepartmentNameValidator(string departmentName)
        {
            DepartmentInfo department = departmentservice.DetailDepartmentdepartment(departmentName);
            if (department != null && department.departmentName == departmentName)
            {
                System.Threading.Thread.Sleep(1000);
                return "error";
            }
            else
            {
                System.Threading.Thread.Sleep(1000);
                return "success";
            }
        }
        #endregion
    }
}
